''' Credit Card check digit checker. The Luhn Algorithm is explained here: https://gizmodo.com/how-credit-card-numbers-work-1493331190 ''' def weighting(digit, weight): n=digit*weight w=(n // 10 + n % 10) return w evenWeight=2 oddWeight=1 t=0 cCardN=[5,4,5,7,6,2,3,8,9,8,2,3,4,1,1,3] #This is a 16 digit credit card number l=(len(cCardN)) for i in range (0, l-1,2): t=t+ weighting(cCardN[i],evenWeight) for i in range (1, l-1,2): t=t+weighting(cCardN[i],oddWeight) remainder = t % 10 calcedCD= (10 - remainder) % 10 if cCardN[l-1] == calcedCD: print("The calculated check digit and the last digit match!") else: print("Oops! The calculated check digit doesn't match the last one read in.")